<template>
  <div class="form-container">
    <el-form inline :model="form" ref="ruleForm" class="form-line" label-width="100">
      <el-form-item label="车辆类型名称" prop="typeName">
        <el-input size="default" v-model="form.typeName" placeholder="请输入车辆类型名称"></el-input>
      </el-form-item>
    </el-form>
    <div class="btn-container">
      <el-button type="primary" @click="handleReset(ruleForm as FormInstance)">重置</el-button>
      <el-button type="primary" @click="handleSearch">查询</el-button>
      <el-button type="primary" @click="handleAdd">添加</el-button>
    </div>
  </div>
  <div class="table-container">
    <el-table
      border
      :cell-style="{ textAlign: 'center', fontSize: '13px' }"
      :header-cell-style="{
        textAlign: 'center',
        fontWeight: '100',
        fontSize: '13px',
        color: 'white',
        backgroundColor: '#009688'
      }"
      :data="tableData"
      style="width: 100%"
    >
      <el-table-column prop="frontalView" label="车辆正面照">
        <template #default="scope">
          <el-image :src="scope.row.frontalView" fit="contain" @click="handleView" style="width: 100px; height: 100px"></el-image>
        </template>
      </el-table-column>
      <el-table-column prop="typeName" label="车辆类型" />
      <el-table-column prop="length" label="可配货规格（CM）" />
      <el-table-column prop="driverLicenseTypeName" label="驾照类型" />
      <el-table-column prop="createTime" label="创建时间" />
      <el-table-column fixed="right" label="操作" width="250">
        <template #default="scope">
          <el-button link type="primary" size="small" @click="handleDelete(scope.row.id)">删除</el-button>
          <el-button link type="primary" size="small" @click="handleEdit(scope.row.id)"> 修改 </el-button>
        </template>
      </el-table-column>
    </el-table>
    <div class="pagination-container">
      <el-pagination
        background
        size="small"
        v-model:current-page="form.pageNum"
        :page-sizes="[5, 10, 15, 20]"
        layout="prev, pager, next"
        :total="pageTotal"
        @change="getCarTypeList"
        :page-size="form.pageSize"
      />
    </div>
  </div>
</template>

<script setup lang="tsx" name="useProTable">
import { ElMessage, ElMessageBox, FormInstance } from "element-plus";
import { ref, onMounted } from "vue";
import { useRouter } from "vue-router";
import { getCarTypeListAPI, CarTypeDeleteAPI } from "@/api/modules/province/carManageAPI";
import { CarType } from "@/api/interface/province/carManage";
import { api as viewerApi } from "v-viewer";
const router = useRouter();
//ProCompanyType 已经通过declare全局申明过不用再单独导入
let tableData = ref<CarType.Row[]>([]);
const images = ref<string[]>([
  "https://img0.baidu.com/it/u=3029360519,385877560&fm=253&fmt=auto&app=120&f=JPEG?w=1193&h=800",
  "https://img0.baidu.com/it/u=3029360519,385877560&fm=253&fmt=auto&app=120&f=JPEG?w=1193&h=800",
  "https://img0.baidu.com/it/u=3029360519,385877560&fm=253&fmt=auto&app=120&f=JPEG?w=1193&h=800",
  "https://img0.baidu.com/it/u=3029360519,385877560&fm=253&fmt=auto&app=120&f=JPEG?w=1193&h=800"
]);
let pageTotal = ref<number>(0);
const form = ref<CarType.SearchParams>({
  typeName: "",
  pageNum: 1,
  pageSize: 10
});
const ruleForm = ref<FormInstance>();
const handleEdit = (id: string) => {
  router.push({
    path: "/provinceLevel/proCar/proCarType/add",
    query: { id }
  });
};
const handleReset = (Elform: FormInstance) => {
  Elform.resetFields();
};
const handleView = () => {
  console.log("执行了函数");
  viewerApi({
    images: images.value
  });
};
const handleSearch = (): void => {
  getCarTypeList();
};
const handleAdd = (): void => {
  router.push({
    path: "/provinceLevel/proCar/proCarType/add"
  });
};
const handleDelete = (id: string): void => {
  ElMessageBox.confirm(`确定删除该条记录吗？`, "提示", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning"
  })
    .then(() => {
      deleteCarType(id);
    })
    .catch(() => {
      ElMessage({
        type: "info",
        message: "已取消删除"
      });
    });
};
// 删除仓站
const deleteCarType = async (id: string) => {
  const { code, msg } = await CarTypeDeleteAPI(id);
  if (code == "200") {
    ElMessage({
      type: "success",
      message: "删除成功!"
    });
    getCarTypeList();
  } else {
    ElMessage.error(`删除失败,${msg}`);
  }
};
// 列表接口请求地址
const getCarTypeList = async () => {
  const { rows, total, code, msg } = await getCarTypeListAPI(form.value);
  console.log(total, code);
  if (code == "200" && rows) {
    tableData.value = rows;
    if (total) pageTotal.value = total;
  } else {
    ElMessage.error(`获取列表失败,${msg}`);
  }
};
onMounted(() => {
  getCarTypeList();
});
</script>
<style lang="scss" scoped>
.form-container {
  padding: 10px;
  margin-bottom: 10px;
  background-color: white;
  .btn-container {
    display: inline-block;
    float: right;
    vertical-align: middle;
  }
  .el-form {
    display: inline-block;
  }
  .form-line {
    .el-form-item {
      margin: 0;
      margin-right: 20px;
    }
    .el-input {
      width: 240px;
    }
    .el-select {
      width: 240px;
    }
  }
}
.table-container {
  padding: 40px 10px;
  padding-bottom: 60px;
  background-color: white;
  .pagination-container {
    // display: inline-block;
    float: right;
    margin-top: 20px;
    background-color: white;
  }
}
::v-deep .el-form-item__label {
  font-size: 13px;
}
::v-deep .el-button > span {
  font-size: 13px;
}
::v-deep .el-input__inner {
  font-size: 13px;
}
</style>
